package com.shanling.shanlingcontroller.ui.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.adapter.GuidePageAdapter;
import com.shanling.shanlingcontroller.base.BaseMVPActivity;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.persenter.GuidePersenter;
import com.shanling.shanlingcontroller.persenter.contract.Guidecontract;
import com.shanling.shanlingcontroller.ui.fragment.GuideFragment;
import com.shanling.shanlingcontroller.utils.netstatus.NetUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class GuideActivity extends BaseMVPActivity<Guidecontract.Presenter> implements Guidecontract.View {

    @BindView(R.id.guide_view_pager)
    ViewPager guideViewPager;
    @BindView(R.id.ll_indicate)
    LinearLayout llIndicate;
    @BindView(R.id.tv_ship)
    TextView tvShip;
    private List<GuideFragment> guideFragments = new ArrayList<>();
    private LinearLayout.LayoutParams dotParams;

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_guide;
    }

    @Override
    protected void initViewsAndEvents() {
        initGuide();
        GuidePageAdapter adapter = new GuidePageAdapter(getSupportFragmentManager(), guideFragments);
        guideViewPager.setAdapter(adapter);
        dotParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        dotParams.leftMargin = 10;
        dotParams.rightMargin = 10;
        initDotViews();
        initViewListener();
    }

    private void initGuide() {
        for (int i = 0; i < 3; i++) {
            guideFragments.add(GuideFragment.buildGuideFragment(i));
        }
    }

    private void initDotViews() {
        if (llIndicate.getChildCount() > 0) {
            return;
        }
        ImageView dotImage;
        for (int i = 0; i < guideFragments.size(); i++) {
            //创建导航页面的图片，并增加到导航布局控件中
            dotImage = new ImageView(mContext);
            if (i == 0) {
                dotImage.setImageResource(R.drawable.ic_guide_point_select);
            } else {
                dotImage.setImageResource(R.drawable.ic_guide_point_normal);
            }
            dotImage.setScaleType(ImageView.ScaleType.CENTER_CROP);
            llIndicate.addView(dotImage, dotParams);
        }
    }

    public void selectNavImage(int position) {
        //获取导航布局中的所有子控件
        ImageView imageView;
        for (int i = 0; i < llIndicate.getChildCount(); i++) {
            imageView = (ImageView) llIndicate.getChildAt(i);    //获取指定位置的子控件
            if (i == position) {
                imageView.setImageResource(R.drawable.ic_guide_point_select);
            } else {
                imageView.setImageResource(R.drawable.ic_guide_point_normal);
            }
        }
    }

    private void initViewListener() {
        guideViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectNavImage(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Override
    protected void initData() {
        mPersenter.getToken();
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }



    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    public void getTokenSuccess(String appToken) {
        preferenceUtil.setAppToken(appToken);
    }
    @Override
    public void getTokenFaild(String msg) {
        Log.e("weiyu", "获取失败" + msg);
    }
    @Override
    protected Guidecontract.Presenter createPresenter() {
        return new GuidePersenter();
    }
    @OnClick(R.id.tv_ship)
    public void onViewClicked() {
        preferenceUtil.setIsFrist(false);
        readyGoThenKill(LoginActivity.class);
    }
}
